-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Site Editor: Add a HMR & React Fast Refresh development mode #26922
Conversation
Size Change: 0 B Total Size: 1.19 MB ℹ️ View Unchanged
|
@@ -0,0 +1,68 @@ | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this config file going into the @wordpress/scripts
package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can skip @wordpress/scripts
for the time being. The existing Gutenberg config is completely independent and way more complex 😅 See: https://github.com/WordPress/gutenberg/blob/master/webpack.config.js. My bet is that we should move all custom logic related to React Fast Refresh there first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for looking at React Fast Refresh. Do you think it would be possible to find a general approach to all pages that use the block editor? This PR covers only the edit site page.
@@ -0,0 +1,68 @@ | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can skip @wordpress/scripts
for the time being. The existing Gutenberg config is completely independent and way more complex 😅 See: https://github.com/WordPress/gutenberg/blob/master/webpack.config.js. My bet is that we should move all custom logic related to React Fast Refresh there first.
// externalize it to take advantage of the | ||
// middleware bootstrapping Core does | ||
// for us. | ||
'@wordpress/api-fetch': 'wp.apiFetch', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's interesting that it removes all externals, does it mean that all wp
globals aren't exposed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means it loads the bundled version instead of the externalized scripts. This makes sure all the changes are hot reloaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can skip @wordpress/scripts for the time being. The existing Gutenberg config is completely independent and way more complex 😅 See: https://github.com/WordPress/gutenberg/blob/master/webpack.config.js. My bet is that we should move all custom logic related to React Fast Refresh there first.
I don't think it's a good idea to mix the two config files. The dev config file is only used for the webpack-dev-server
and nothing else throughout the build process. Therefore it would be better to keep them separated.
Block editors are initialized by WordPress injecting inline scripts to the page. This prevents us from using HMR, that's why we have a separate On the other hand, post editor is initialized in WordPress core: https://github.com/WordPress/WordPress/blob/5da8c0fcebf0b7cab6d1821be8f464f4428f942f/wp-admin/edit-form-blocks.php#L417-L425 which makes things even more complicated. We can add HMR to Widgets and Navigation editor the same way we do for the Site editor, but the post editor is complicated since we don't have any filters that we could use to manipulate the initialization of the editor. |
It's a very challenging task to make React Fast Refresh work with WordPress in general. It might work at the moment to ignore the
|
I'm pretty sure we have limitations on how far we can go. On the other hand, HMR works well if we are developing in the core. It'd save us a lot of time. Refreshing after all the changes vs refreshing after 10% of the changes. We could run this as an experimental feature and see how useful it is. If it's breaking too many times then we can revert it anytime. |
Not sure if this is useful, but devs from Vue, Preact, and Snowpack collaborated to make a bundler-independent HMR spec. It looks like the only implementation so far is Snowpack's, though. #25077 is somewhat related, since that could -- but wouldn't have to -- involve switching to a modern bundler. That might make this easier (including beyond the Site Editor, so that plugins could use it too). |
See #28273 (comment). I have a working proof of concepts for React Fast Refresh with |
Description
Related to #23013. This PR only adds HMR to the Site Editor. Unfortunately, the original PR has been abandoned: #23013. Big-big-big thanks to @epiqueras for working on this, hopefully, we can get this to the finish line this time!
Original PR had a few issues that have been fixed here. Happy to introduce this to
edit-post
and all the other editors once we got this into stabilized and merged.Background
Webpack Hot Module Replacement (HMR) is fundamentally at odds with the way we do script overriding in Gutenberg.
After lots of experimentation and failed attempts, I found that the best way to support it is to provide an alternative development mode on a per-page/app basis. For example, the site editor is its own page/app, and this PR enables an HMR mode for its development.
It should be trivial to reuse the same configurations for the post, widgets, and navigation editors.
I also included the new React Fast Refresh experience. The developer experience with this is fantastic. Even error resilience and recovery are flawless, both for runtime and syntax errors. It's nice to be able to iterate on a screen without resetting state.
Implementation
This PR introduces a new Webpack config for running the new dev server as an extension of the one provided by
@wordpress/scripts
.Pages that wish to support an HMR mode, need to create a
hot.js
entry point like this PR does foredit-site
. This entry point should initialize the page and load its root stylesheet.On the server, we use a new config global,
GUTENBERG_HMR
, to determine whether to load the new script from the dev server or not. We manipulate this config variable effortlessly in the scripts introduced by this PR by usingwp-env
. It will enable it before running the new development mode, and disable it before running the old one. This makes it easy to switch between the two, but it does assume you are usingwp-env
to manage your environment.How to test this?
wp-env
if you haven't yet.npm run dev:edit-site
.Screenshots
Checklist: